package com.cqc.zxing01;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.cqc.zxing01.zxing.encode.EncodingHandler;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${cqc} on 2017/4/1.
 */

public class CreateQrCodeActivity extends AppCompatActivity {

    private Context context;
    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.iv)
    ImageView iv;
    private Bitmap barCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_qr_code);
        ButterKnife.bind(this);
        context = this;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (barCode != null && !barCode.isRecycled()) {
            barCode.recycle();
        }
    }

    @OnClick({R.id.btn, R.id.btn2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn:
                String text = et.getText().toString().trim();
                if (TextUtils.isEmpty(text)) {
                    btn.setError("内容不能为空");
                    return;
                }
                try {
                    barCode = EncodingHandler.create2Code(text, 600);
                    iv.setImageBitmap(barCode);
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtil.showShortToast(context, "未生成一维码");
                }
                break;
            case R.id.btn2:
                //如果回收的话，当activity亚茹后台的再进来的时候会报错
                ToastUtil.showShortToast(context, "回收");
                if (barCode != null && !barCode.isRecycled()) {
                    barCode.recycle();
                }
                break;
        }
    }
}
